package hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import hibernate.entity.ClassRoom;
import hibernate.entity.News;
import hibernate.relation.onetoone.Employee;
import hibernate.relation.onetoone.Word;

public class test {
	public static void main(String[] args) {
		//1.创建一个SessionFactory对象
		SessionFactory sessionFactory=null;
		//1)创建Configuration对象：对应hibernate的基本配置信息和对象关系映射信息
		Configuration configuration=new Configuration().configure();
		//4.0以前这样创建
		//sessionFactory=configuration.buildSessionFactory();
		//2)创建一个ServiceRegistry对象：hibernate 4.x新添加的对象
		//hibernate的任何配置和服务都需要在该对象中注册后才能有效
		//ServiceRegistry serviceRegistry= new ServiceRegistrybuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		//在hibernate5.x中buildServiceRegistry方法被替换掉了我们使用如下方式去创建ServiceRegistry对象
		StandardServiceRegistry serviceRegistry=  new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
		//3)
		sessionFactory=configuration.buildSessionFactory();
		//2.创建一个Session对象
		Session session=sessionFactory.openSession();
		//3.开启事务
		Transaction transaction= session.beginTransaction();
		//4.执行保存操作
/*		Employee employee=new Employee();
		employee.setEname("AA");
		employee.setBirthday(new Date());
		session.save(employee);
		Word word=new Word();
		word.setEmp(employee);
		word.setContent("sssssss");
		word.setTitle("qqqq");
		session.save(word);*/
		Employee e=session.get(Employee.class, 3);
		System.out.println(e.toString());
		
		//5.提交事务
		transaction.commit();
		//6.关闭session
		session.close();
		//7.关闭SessionFactory对象
		sessionFactory.close();
	}
}
